package com.hnevc.foodserverapi.controller;


import com.hnevc.foodserverapi.pojo.Users;
import com.hnevc.foodserverapi.service.UsersService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class UserController {
    @Autowired
    private UsersService usersService;

    /**
     * 登录API
     * 参数类型javabean User的json对象
     * 返回类型json，无数据返回
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> login(@RequestBody Users user) {
        List<Users> login = usersService.login(user);
        Map<String, Object> map = new HashMap<>();
        if (!login.isEmpty()) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", login);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @RequestMapping(value = "/login2",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> login(@RequestParam String telephone,@RequestParam String password) {

        Map<String, Object> map = new HashMap<>();

        Users user=new Users();
        user.setTelephone(telephone);
        user.setPassword(password);

        List<Users> login = usersService.login(user);
        if (!login.isEmpty()) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", login);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }



    /**
     * 注册API
     * 参数类型javabean User的json对象
     * 返回类型json，无数据返回
     */
    @PostMapping("/register")
    @ResponseBody
    public Map<String,Object> register(@RequestBody Users user){
        int register = usersService.register(user);
        Map<String,Object> map = new HashMap<>();
        if (register<=0){
            map.put("success",false);
            map.put("msg","失败");
        }else {
            map.put("success",true);
            map.put("msg","成功");
        }
        return map;
    }
}
